

library(tidyverse)
library(cepespR)
library(readxl)
library(data.table)
library(stargazer)
library(haven)

#usar os arquivos "Estadual" e "Federal" na pasta

load("Federal.RData")
load("Estadual.RData")

MUNIC_UF <- data.table(UF = c("MG","SP","RS","BA","PR","SC","GO","PI", 
                              "PB","MA","PE","CE","RN","PA",
                              "MT","TO","AL","RJ","MS","ES",
                              "SE","AM","RO","AC","AP","RR"),  
                       MUNIC = c(853,645,497,417,399,295,246,224,
                                 223,217,185,184,167,144,141,139,
                                 102,92,79,78,75,62,52,22,16,15)) 

#deputadofederal2010
Tabela <- FN_f %>% filter(ANO_ELEICAO==2010)%>%
  group_by(Codigo_Resi, Partido,capitais,UF) %>%
  summarise(Valor = sum(Dep_Federal))%>%
  spread(Partido,Valor)

Tabela [is.na(Tabela)] = 0
Codigo_Resi_C<-Tabela[1:2]

Tabela1<-Tabela

Tabela1[4:28]<-ifelse(Tabela1[4:28]>=1,1,0)


Part<-rowSums(Tabela1[4:28])
Part<-tibble(Part)
Tabela1<-cbind(Tabela1,Part)

Cand<-rowSums(Tabela[4:28])
Cand<-tibble(Cand)
Tabela<-cbind(Tabela,Cand)
Tabela<-left_join(Tabela,MUNIC_UF,by="UF")
Cand1<-Tabela$Cand/Tabela$MUNIC
Cand1<-tibble(Cand1)
Tabela<-cbind(Tabela,Cand1)


F2010<-cbind(Codigo_Resi_C,Cand1,Part)
F2010C<-filter(F2010,capitais==1)
F2010S<-filter(F2010,capitais==0)
rm(Codigo_Resi_C,Cand,Cand1,F2010,Part,Tabela,Tabela1)

#deputadofederal2014
Tabela <- FN_f %>% filter(ANO_ELEICAO==2014)%>%
  group_by(Codigo_Resi, Partido,capitais,UF) %>%
  summarise(Valor = sum(Dep_Federal))%>%
  spread(Partido,Valor)

Tabela [is.na(Tabela)] = 0
Codigo_Resi_C<-Tabela[1:2]

Tabela1<-Tabela

Tabela1[4:33]<-ifelse(Tabela1[4:33]>=1,1,0)


Part<-rowSums(Tabela1[4:33])
Part<-tibble(Part)
Tabela1<-cbind(Tabela1,Part)

Cand<-rowSums(Tabela[4:33])
Cand<-tibble(Cand)
Tabela<-cbind(Tabela,Cand)
Tabela<-left_join(Tabela,MUNIC_UF,by="UF")
Cand1<-Tabela$Cand/Tabela$MUNIC
Cand1<-tibble(Cand1)
Tabela<-cbind(Tabela,Cand1)

F2014<-cbind(Codigo_Resi_C,Cand1,Part)
F2014C<-filter(F2014,capitais==1)
F2014S<-filter(F2014,capitais==0)
rm(Codigo_Resi_C,Cand,Cand1,F2014,Part,Tabela,Tabela1)

#deputadofederal2018
Tabela <- FN_f %>% filter(ANO_ELEICAO==2018)%>%
  group_by(Codigo_Resi, Partido,capitais,UF) %>%
  summarise(Valor = sum(Dep_Federal))%>%
  spread(Partido,Valor)

Tabela [is.na(Tabela)] = 0
Codigo_Resi_C<-Tabela[1:2]

Tabela1<-Tabela

Tabela1[4:35]<-ifelse(Tabela1[4:35]>=1,1,0)


Part<-rowSums(Tabela1[4:35])
Part<-tibble(Part)
Tabela1<-cbind(Tabela1,Part)

Cand<-rowSums(Tabela[4:35])
Cand<-tibble(Cand)
Tabela<-cbind(Tabela,Cand)
Tabela<-left_join(Tabela,MUNIC_UF,by="UF")
Cand1<-Tabela$Cand/Tabela$MUNIC
Cand1<-tibble(Cand1)
Tabela<-cbind(Tabela,Cand1)

F2018<-cbind(Codigo_Resi_C,Cand1,Part)
F2018C<-filter(F2018,capitais==1)
F2018S<-filter(F2018,capitais==0)
rm(Codigo_Resi_C,Cand,Cand1,F2018,Part,Tabela,Tabela1)

#deputadoestadual2010
FN_e$estadual<-1
Tabela <- FN_e %>% filter(ANO_ELEICAO==2010)%>%
  group_by(Codigo_Resi, Partido,capitais,UF) %>%
  summarise(Valor = sum(estadual))%>%
  spread(Partido,Valor)

Tabela [is.na(Tabela)] = 0
Codigo_Resi_C<-Tabela[1:2]

Tabela1<-Tabela

Tabela1[4:28]<-ifelse(Tabela1[4:28]>=1,1,0)


Part<-rowSums(Tabela1[4:28])
Part<-tibble(Part)
Tabela1<-cbind(Tabela1,Part)

Cand<-rowSums(Tabela[4:28])
Cand<-tibble(Cand)
Tabela<-cbind(Tabela,Cand)
Tabela<-left_join(Tabela,MUNIC_UF,by="UF")
Cand1<-Tabela$Cand/Tabela$MUNIC
Cand1<-tibble(Cand1)
Tabela<-cbind(Tabela,Cand1)

E2010<-cbind(Codigo_Resi_C,Cand1,Part)
E2010C<-filter(E2010,capitais==1)
E2010S<-filter(E2010,capitais==0)
rm(Codigo_Resi_C,Cand,Cand1,E2010,Part,Tabela,Tabela1)

#deputadoestadual2014
Tabela <- FN_e %>% filter(ANO_ELEICAO==2014)%>%
  group_by(Codigo_Resi, Partido,capitais,UF) %>%
  summarise(Valor = sum(estadual))%>%
  spread(Partido,Valor)

Tabela [is.na(Tabela)] = 0
Codigo_Resi_C<-Tabela[1:2]

Tabela1<-Tabela

Tabela1[4:34]<-ifelse(Tabela1[4:34]>=1,1,0)


Part<-rowSums(Tabela1[4:34])
Part<-tibble(Part)
Tabela1<-cbind(Tabela1,Part)

Cand<-rowSums(Tabela[4:34])
Cand<-tibble(Cand)
Tabela<-cbind(Tabela,Cand)
Tabela<-left_join(Tabela,MUNIC_UF,by="UF")
Cand1<-Tabela$Cand/Tabela$MUNIC
Cand1<-tibble(Cand1)
Tabela<-cbind(Tabela,Cand1)

E2014<-cbind(Codigo_Resi_C,Cand1,Part)
E2014C<-filter(E2014,capitais==1)
E2014S<-filter(E2014,capitais==0)
rm(Codigo_Resi_C,Cand,Cand1,E2014,Part,Tabela,Tabela1)

#deputadoestadual2018
Tabela <- FN_e %>% filter(ANO_ELEICAO==2018)%>%
  group_by(Codigo_Resi, Partido,capitais,UF) %>%
  summarise(Valor = sum(estadual))%>%
  spread(Partido,Valor)

Tabela [is.na(Tabela)] = 0
Codigo_Resi_C<-Tabela[1:2]

Tabela1<-Tabela

Tabela1[4:36]<-ifelse(Tabela1[4:36]>=1,1,0)


Part<-rowSums(Tabela1[4:36])
Part<-tibble(Part)
Tabela1<-cbind(Tabela1,Part)

Cand<-rowSums(Tabela[4:36])
Cand<-tibble(Cand)
Tabela<-cbind(Tabela,Cand)
Tabela<-left_join(Tabela,MUNIC_UF,by="UF")
Cand1<-Tabela$Cand/Tabela$MUNIC
Cand1<-tibble(Cand1)
Tabela<-cbind(Tabela,Cand1)

E2018<-cbind(Codigo_Resi_C,Cand1,Part)
E2018C<-filter(E2018,capitais==1)
E2018S<-filter(E2018,capitais==0)
rm(Codigo_Resi_C,Cand,Cand1,E2018,Part,Tabela,Tabela1)




a<-summary(F2010S$Cand1)
b<-summary(F2010S$Part)

c<-summary(F2010C$Cand1)
d<-summary(F2010C$Part)

federal2010<-rbind(a,b,c,d)
federal2010<-round(federal2010, digits = 1)
federal2010<-as_tibble(federal2010)
federal2010<-select(federal2010,"1st Qu.":"3rd Qu.")
federal2010<-t(federal2010)




a<-summary(F2014S$Cand1)
b<-summary(F2014S$Part)

c<-summary(F2014C$Cand1)
d<-summary(F2014C$Part)

federal2014<-rbind(a,b,c,d)
federal2014<-round(federal2014, digits = 1)
federal2014<-as_tibble(federal2014)
federal2014<-select(federal2014,"1st Qu.":"3rd Qu.")
federal2014<-t(federal2014)


a<-summary(F2018S$Cand1)
b<-summary(F2018S$Part)

c<-summary(F2018C$Cand1)
d<-summary(F2018C$Part)

federal2018<-rbind(a,b,c,d)
federal2018<-round(federal2018, digits = 1)
federal2018<-as_tibble(federal2018)
federal2018<-select(federal2018,"1st Qu.":"3rd Qu.")
federal2018<-t(federal2018)


federal<-cbind(federal2010,federal2014,federal2018)
federal<-as.data.frame(federal)
federal<-rename(federal,Cand_N_2010=V1)
federal<-rename(federal,Part_N_2010=V2)
federal<-rename(federal,Cand_C_2010=V3)
federal<-rename(federal,Part_C_2010=V4)
federal<-rename(federal,Cand_N_2014=V5)
federal<-rename(federal,Part_N_2014=V6)
federal<-rename(federal,Cand_C_2014=V7)
federal<-rename(federal,Part_C_2014=V8)
federal<-rename(federal,Cand_N_2018=V9)
federal<-rename(federal,Part_N_2018=V10)
federal<-rename(federal,Cand_C_2018=V11)
federal<-rename(federal,Part_C_2018=V12)
Tabela9F<-federal



#estadual


a<-summary(E2010S$Cand1)
b<-summary(E2010S$Part)

c<-summary(E2010C$Cand1)
d<-summary(E2010C$Part)

estadual2010<-rbind(a,b,c,d)
estadual2010<-round(estadual2010, digits = 1)
estadual2010<-as_tibble(estadual2010)
estadual2010<-select(estadual2010,"1st Qu.":"3rd Qu.")
estadual2010<-t(estadual2010)

a<-summary(E2014S$Cand1)
b<-summary(E2014S$Part)

c<-summary(E2014C$Cand1)
d<-summary(E2014C$Part)


estadual2014<-rbind(a,b,c,d)
estadual2014<-round(estadual2014, digits = 1)
estadual2014<-as_tibble(estadual2014)
estadual2014<-select(estadual2014,"1st Qu.":"3rd Qu.")
estadual2014<-t(estadual2014)




a<-summary(E2018S$Cand1)
b<-summary(E2018S$Part)

c<-summary(E2018C$Cand1)
d<-summary(E2018C$Part)

estadual2018<-rbind(a,b,c,d)
estadual2018<-round(estadual2018, digits = 1)
estadual2018<-as_tibble(estadual2018)
estadual2018<-select(estadual2018,"1st Qu.":"3rd Qu.")
estadual2018<-t(estadual2018)


estadual<-cbind(estadual2014,estadual2014,estadual2018)
estadual<-as.data.frame(estadual)
estadual<-rename(estadual,Cand_N_2010=V1)
estadual<-rename(estadual,Part_N_2010=V2)
estadual<-rename(estadual,Cand_C_2010=V3)
estadual<-rename(estadual,Part_C_2010=V4)
estadual<-rename(estadual,Cand_N_2014=V5)
estadual<-rename(estadual,Part_N_2014=V6)
estadual<-rename(estadual,Cand_C_2014=V7)
estadual<-rename(estadual,Part_C_2014=V8)
estadual<-rename(estadual,Cand_N_2018=V9)
estadual<-rename(estadual,Part_N_2018=V10)
estadual<-rename(estadual,Cand_C_2018=V11)
estadual<-rename(estadual,Part_C_2018=V12)
Tabela9E<-estadual

rm(E2010C,E2010S,E2014C,E2014S,E2018C,E2018S,estadual2010,estadual2014,estadual2018)
rm(F2010C,F2010S,F2014C,F2014S,F2018C,F2018S,federal2010,federal2014,federal2018)
rm(FN_e,FN_f,MUNIC_UF,estadual,federal)
